package com.jx.lcs.productbill.web;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jx.lcs.productbill.entity.ProductBill;
import com.jx.lcs.productbill.service.ProductBillService;
import com.jx.lcs.util.file.FileUtil;
import com.tianyu.jty.common.persistence.Page;
import com.tianyu.jty.common.web.BaseController;

/** 
 * 产品关单
 * @author JiaYY 
 * @date 2015年7月15日
 */
@Controller
@RequestMapping("productBill")
public class ProductBillController extends BaseController {

	private static final Logger logger = LoggerFactory.getLogger(ProductBillController.class);
	
	@Autowired
	private ProductBillService productBillService;
	
	/**
	 * 默认页面
	 */
	@RequestMapping(method = RequestMethod.GET)
	public String list() {
		return "productbill/productBillList";
	}
	
	/**
	 * 获取产品关单列表json
	 */
	@RequestMapping(value="json",method = RequestMethod.GET)
	@ResponseBody
	public Map<String, Object> productBillList(HttpServletRequest request) {
		try{
			Page<ProductBill> page = getPage(request);
			page = productBillService.findProductBillList(page, request);
			return getEasyUIData(page);
		}catch(Exception e){
			logger.info("--获取产品关单列表出现异常！", e);
			return null;
		}
	}
	
	/**
	 * 查看上传凭证
	 * 
	 * @param id
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "viewCard/{id}", method = RequestMethod.GET)
	public String viewCard(@PathVariable("id") String id, Model model) {
		model.addAttribute("productBill", productBillService.get(id));
		return "productbill/productbillCard";
	}
	
	/**
	 * 审核凭证
	 * 
	 * @param id
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "examCard/{id}", method = RequestMethod.GET)
	public String examCard(@PathVariable("id") String id, String status, Model model) {
		try{
			String result = productBillService.examCard(id, status);
			if(result != null) 
				return result;
			
			return "success";
		} catch(Exception e) {
			logger.error("--关单审核出现异常", e);
			return "error";
		}
	}
	
	/**
	 * 下载上传凭证图片
	 * @param imgUrl
	 * @param response
	 */
	@RequestMapping(value="downloadCardImg")
	public void downloadCardImg(String imgUrl, HttpServletResponse response) {
		FileUtil.downloadFile(imgUrl, response);
	}
	
	/**
	 * 预览图片 
	 * @param imgUrl
	 * @param model
	 */
	@RequestMapping(value="preview")
	public String preview(String imgUrl, Model model) {
		model.addAttribute("imgUrl", imgUrl);
		return "productbill/previewCard";
	}
}
